<?php
class Calls extends Model{
	
	function getAll(){
		$sql ="SELECT *,c.id,c.date_stamp FROM calls c ";
		$sql.="LEFT JOIN users u ON u.id=c.user_id ";
		$sql.="LEFT JOIN user_position p ON p.id=u.pos_id ";
        if(($this->session->userdata('user_group')== $this->config->item('user')) &&  ($this->session->userdata('user_pos')== $this->config->item('ta')) ): 
            $sql.="WHERE u.username='".$this->session->userdata('user_id')."'";
        endif;
		$query=$this->db->query($sql);
		return $query->result_array();
	}
    
    function getAllUsers(){
		$sql =" SELECT * FROM users u ";
		$sql.=" LEFT JOIN user_group g ON g.id=u.group_id ";
		$sql.=" LEFT JOIN user_position p ON p.id=u.pos_id ";
        $sql.=" WHERE u.group_id=".$this->config->item('user');
		$sql.=" AND u.pos_id=".$this->config->item('ta');
        $sql.=" AND u.enabled=1 AND sales=1 ";
        $query=$this->db->query($sql);
		return $query->result_array();
	}
    
    /*function getCek(){
        $sql ="SELECT * FROM calls u ";
		$sql.="WHERE id = '".$this->input->post('no')."' LIMIT 1";
        $query=$this->db->query($sql);
		return $query->row_array();
    }*/
    
    function getData($id){
		$sql ="SELECT *,c.id FROM calls c ";
		$sql.="LEFT JOIN users u ON u.id=c.user_id ";
		$sql.="LEFT JOIN user_position p ON p.id=u.pos_id ";
        $sql.="WHERE c.id = '".$id."' LIMIT 1";
		$query=$this->db->query($sql);
		return $query->row_array();
	}
    
    function getSave(){
        $this->db->where('username',$this->input->post('user_id'));
        $Q=$this->db->get('users');
        $user=$Q->row_array();
        $user=$user['id']; 
        $value = array  (
                            'call_name'     =>  $this->input->post('call'),
                            'user_id'       =>  $user,
                            'contact_name'  =>  $this->input->post('name'),
                            'contact_number'=>  $this->input->post('tlp'),
                            'contact_result'=>  $this->input->post('result'),
                            'user_stamp'=>  $this->session->userdata('user_id'),
                            'date_stamp'=>  date('Y-m-d')
                        );
        $this->db->insert('calls',$value);                
    }
    
    function getUpdate(){
        $this->db->where('username',$this->input->post('user_id'));
        $Q=$this->db->get('users');
        $user=$Q->row_array();
        $user=$user['id'];
        
        $value = array  (
                            'call_name'     =>  $this->input->post('call'),
                            'user_id'       =>  $user,
                            'contact_name'  =>  $this->input->post('name'),
                            'contact_number'=>  $this->input->post('tlp'),
                            'contact_result'=>  $this->input->post('result'),
                            'user_stamp'=>  $this->session->userdata('user_id'),
                            'date_stamp'=>  date('Y-m-d')
                        );
        $this->db->where('id',$this->input->post('id'));                 
        $this->db->update('calls',$value);                
    }
    
    function getRemove($id){
        $this->db->where('id',$id);
        $this->db->delete('calls');
    }
}